package com.liuyun.sunadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuyun.sunadmin.pojo.AdminUser;
import com.liuyun.sunadmin.mapper.AdminUserMapper;
import com.liuyun.sunadmin.query.AdminQuery;
import com.liuyun.sunadmin.service.IAdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuyun.sunadmin.utils.AssertUtil;
import com.liuyun.sunadmin.utils.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 超管用户记录表 服务实现类
 * </p>
 *
 * @author 老傅xsx
 * @since 2021-09-14
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {

    @Override
    public AdminUser findUserByUserName(String userName) {

        return this.baseMapper.selectOne(new QueryWrapper<AdminUser>().eq("user_name", userName));
    }



    @Override
    public void login(AdminUser user) {
        AssertUtil.isTrue(StringUtils.isBlank(user.getUserName()),"用户名不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(user.getPassword()),"密码不能为空");

        AdminUser temp = this.findUserByUserName(user.getUserName());

        AssertUtil.isTrue(null == temp, "用户不存在!");

        AssertUtil.isTrue(!StringUtils.equals(user.getPassword(),temp.getPassword()),"密码错误");

    }

    @Override
    public Map<String, Object> adminList(AdminQuery adminQuery) {
        List<AdminUser> menus = this.list(new QueryWrapper<AdminUser>());
        return PageResultUtil.getResult((long) menus.size(),menus);
    }
}
